<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\shiyao;

use app\common\model\shiyao\ProjectTestee as ProjectTesteeModel;
use think\facade\Db;

/**
 * 受试者模型
 * Class StoreUser
 * @package app\store\model
 */
class ProjectTestee extends ProjectTesteeModel {

	public function getListAll($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => [], 'centerId' => '', 'logids' => []]);
		$filter = [];

		!empty($params['centerId']) && $query->where('testee.center_id', '=', $params['centerId']);

		if (!empty($params['logids'])) {
			$query->where('testee.testee_id', 'in', $params['logids']);
		}

		// 查询列表记录
		$list = $query->alias('testee')
			->join('shiyao_project p', 'p.project_id=testee.project_id')
			->join('shiyao_company co', 'co.company_id=p.company_id')
			->join('shiyao_project_center c', 'c.id=testee.center_id')
			->where('testee.is_delete', '=', '0')
			->where($filter)
			->field('testee.*,p.project_name,p.project_code,c.center_code,c.center_name,co.company_name')

			->order(['create_time' => 'desc'])
			->select()->toArray();

		foreach ($list as &$item) {
			$item['center_code'] = $item['center_code'] . '/' . $item['center_name'];
		}

		return $list;
	}

	/**
	 * 获取用户列表
	 * @param array $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 */
	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => [], 'centerId' => '', 'projectId' => '']);
		$filter = [];
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}

		!empty($params['centerId']) && $query->where('p.center_id', '=', $params['centerId']);
		!empty($params['userId']) && $query->where('p.user_id', '=', $params['userId']);
		!empty($params['projectId']) && $query->where('p.project_id', '=', $params['projectId']);

		// 关键词搜索
		!empty($params['search']) && $query->where('p.code', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')->with(['idcardAvatar', 'idcardFile', 'bank', 'certFile'])

			->where($filter)
			->where('p.is_delete', '=', '0')
			->order(['p.create_time' => 'desc'])
			->paginate(15);

		return $list;
	}
	public function getListUser($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => [], 'centerId' => '', 'projectId' => '']);
		$filter = [];
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}

		!empty($params['centerId']) && $query->where('p.center_id', '=', $params['centerId']);
		!empty($params['userId']) && $query->where('p.user_id', '=', $params['userId']);
		!empty($params['projectId']) && $query->where('p.project_id', '=', $params['projectId']);

		// 关键词搜索
		!empty($params['search']) && $query->where('p.code', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')->with(['idcardAvatar', 'idcardFile', 'bank', 'certFile'])
			->join('shiyao_project_center center', 'center.id=p.center_id')
			->join('shiyao_project pr', 'pr.project_id=p.project_id')
			->join('shiyao_company c', 'c.company_id=pr.company_id')
			->where($filter)
			->where('p.is_delete', '=', '0')
			->field('p.*,pr.*,c.*,center.center_name,center.center_code,center.create_time,center.pm_id')
			->order(['center.create_time' => 'desc'])
			->paginate(15)->each(function ($item, $key) {

			$item['pms'] = Db::name('shiyao_project_center_pm')->alias('pm')
				->join('user', 'user.user_id=pm.pm_id')
				->where('pm.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile')->select()->toArray();
			$item['pi'] = Db::name('shiyao_project_center_pi')->alias('pi')
				->join('user', 'user.user_id=pi.user_id')
				->where('pi.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,pi.*')->select()->toArray();
			$item['crc'] = Db::name('shiyao_project_center_crc')->alias('crc')
				->join('user', 'user.user_id=crc.user_id')
				->where('crc.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,crc.*')->select()->toArray();

			$item['pm'] = Db::name('user')->where('user_id', $item['pm_id'])->field('mobile,nick_name')->find();

			$item['testee_count'] = Db::name('shiyao_project_center_testee')
				->where('center_id', $item['center_id'])->count();
			$item['buzhu_count'] = Db::name('shiyao_project_pay_log')
				->where('center_id', $item['center_id'])->count();
			return $item;
		});

		return $list;
	}

	public function getListUserAll($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['search' => '', 'betweenTime' => [], 'centerId' => '', 'projectId' => '']);
		$filter = [];
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}

		!empty($params['centerId']) && $query->where('p.center_id', '=', $params['centerId']);
		!empty($params['userId']) && $query->where('p.user_id', '=', $params['userId']);
		!empty($params['projectId']) && $query->where('p.project_id', '=', $params['projectId']);

		// 关键词搜索
		!empty($params['search']) && $query->where('p.code', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')->with(['idcardAvatar', 'idcardFile', 'bank', 'certFile'])
			->join('shiyao_project_center center', 'center.id=p.center_id')
			->join('shiyao_project pr', 'pr.project_id=p.project_id')
			->join('shiyao_company c', 'c.company_id=pr.company_id')
			->where($filter)
			->where('p.is_delete', '=', '0')
			->field('p.*,pr.*,c.*,center.center_name,center.center_code,center.create_time,center.pm_id')
			->order(['center.create_time' => 'desc'])
			->select()->toArray();
		foreach ($list as &$item) {
			$item['pms'] = Db::name('shiyao_project_center_pm')->alias('pm')
				->join('user', 'user.user_id=pm.pm_id')
				->where('pm.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile')->select()->toArray();
			$item['pi'] = Db::name('shiyao_project_center_pi')->alias('pi')
				->join('user', 'user.user_id=pi.user_id')
				->where('pi.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,pi.*')->select()->toArray();
			$item['crc'] = Db::name('shiyao_project_center_crc')->alias('crc')
				->join('user', 'user.user_id=crc.user_id')
				->where('crc.center_id', $item['center_id'])
				->field('user.nick_name,user.mobile,crc.*')->select()->toArray();

			$item['pm'] = Db::name('user')->where('user_id', $item['pm_id'])->field('mobile,nick_name')->find();

			$item['testee_count'] = Db::name('shiyao_project_center_testee')
				->where('center_id', $item['center_id'])->count();
			$item['buzhu_count'] = Db::name('shiyao_project_pay_log')
				->where('center_id', $item['center_id'])->count();
		}

		return $list;
	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {

		$data['store_id'] = self::$storeId;

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增
			$this->save($data);

		});
		return true;
	}

	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {

		$data['icf_date'] = strtotime($data['icf_date']);

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	public function audit(array $data) {

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	public function setDelete(array $testeeIds) {

		foreach ($testeeIds as $centerId) {
			$log = Db::name('shiyao_project_pay_log')->where('testee_id', $centerId)->find();
			if ($log) {

				$this->error = '当前受试者有补助记录，不允许删除';
				return false;
			}

		}

		Db::name('shiyao_project_center_testee')->delete($centerIds);
		// 批量更新记录
		return true;

	}

	public static function detail(int $testeeId) {

		$detail = self::get(['testee_id' => $testeeId], ['idcardAvatar', 'idcardFile', 'bank', 'certFile']);

		return $detail;
	}

}
